package grafics;


import java.awt.Color;
import java.awt.Image;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;



public class BitmapRead {
	private static BufferedImage image;
	private static BufferedImageBuilder build=new BufferedImageBuilder();
	private static HashMap<Integer,DateValue> hashMap  = new HashMap<Integer,DateValue>(); 
	private static int max=0;
	
    public static String GeneratePlot(Connection conn,String plot,String senzor) {
    	String returnString=null;
    	max=getData(conn,plot,senzor);
    	createBitMap(plot,max);
    	returnString=generateOutput(plot);
    	
    	return returnString;
	}
    
    private static int getData(Connection conn,String plot,String senzor)
    {
    	Connection connection=conn;
    	int index=1;
    	int maxim=0;    	
    	
	try{		

        Statement statement= connection.createStatement();                    
        ResultSet result=statement.executeQuery("select ora, valoare, senzor from scenariu where senzor like '"+senzor+"' order by ora ");           
		int i=0;
		while (result.next())
		{
			i++;
			hashMap.put(index,new DateValue(i,10*result.getInt(2)));
			index++;
		}
            maxim=max(hashMap)+25;
            image=new BufferedImage(80+hashMap.get(hashMap.size()).getDate()+20,maxim,BufferedImage.TYPE_INT_RGB);
	
	}
	
	catch (SQLException e) {
		e.printStackTrace();
   }
   	return maxim;
}
    
    private static void createBitMap(String plot,int max)
    {   
    	for(int y = 0; y < image.getHeight(); y++){
			 for(int x = 0; x < image.getWidth(); x++){
				image.setRGB(x, y, -1);
			    }
			}
        max=max-10;
        for(int it=3; it<hashMap.size()-3;it++)
        {
        	for(int x=1;x<=2;x++)
        		for(int y=1;y<=2;y++){
        			
        			
        			line(78+(Integer)hashMap.get(it-1).getDate()+y,max-(hashMap.get(it-1).getValue()+x+10),78+hashMap.get(it).getDate()+y,max-(x+hashMap.get(it).getValue()+10), Color.RED.getRGB(),image);
        		}
        }
    }    
    private static String generateOutput(String plot)
    {
         String fileName;
         int scale=40;
         fileName=new String(Library.getLibraryPath()+"\\"+Math.random()+plot+".gif");
         File outputfilex = new File(fileName);
         try {
			ImageIO.write(image, "gif", outputfilex);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
         Image transitImage=BitmapRead.image.getScaledInstance(960,400,Image.SCALE_SMOOTH);         
         BufferedImage outputImage= BitmapRead.build.bufferImage(transitImage);
         drawScale(outputImage,scale);       
         drawDate(outputImage,Color.BLACK,15);
         fileName=new String(Library.getLibraryPath()+"\\"+plot+".gif");
         File outputfile = new File(fileName);
         try {
			ImageIO.write(outputImage, "gif", outputfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
         try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
     	return fileName;
    }
    
    private static void line(int x,int y,int x2, int y2, int color, BufferedImage img) {
	    int w = x2 - x ;
	    int h = y2 - y ;
	    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
	    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
	    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
	    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
	    int longest = Math.abs(w) ;
	    int shortest = Math.abs(h) ;
	    if (!(longest>shortest)) {
	        longest = Math.abs(h) ;
	        shortest = Math.abs(w) ;
	        if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
	        dx2 = 0 ;            
	    }
	    int numerator = longest >> 1 ;
	    for (int i=0;i<=longest;i++) {
	    	img.setRGB(x,y,color) ;
	        numerator += shortest ;
	        if (!(numerator<longest)) {
	            numerator -= longest ;
	            x += dx1 ;
	            y += dy1 ;
	        } else {
	            x += dx2 ;
	            y += dy2 ;
	        }
	    }
    } 
    
private static int max(HashMap<Integer,DateValue> hash)
{
	int max=0;
	for(int i=1;i<hash.size();i++)
		if (hash.get(i).getValue() > max ) max=hash.get(i).getValue(); 
	return max;
}

public static void drawScale(BufferedImage img,int scale)
{
	int hour1 =1;
	int hour2 =1;
	Font font = new Font("Arial", Font.PLAIN, 10);
	Graphics g=img.getGraphics();
	g.setColor(Color.LIGHT_GRAY);
	g.setFont(font);	
	for(int x=0; x<img.getWidth();x++){
		img.setRGB(x, 0,Color.LIGHT_GRAY.getRGB() );
		img.setRGB(x, img.getHeight()-1,Color.LIGHT_GRAY.getRGB() );
	}
	for(int y=0; y<img.getHeight();y++){
		img.setRGB(0, y,Color.LIGHT_GRAY.getRGB() );
		img.setRGB(img.getWidth()-1,y,Color.LIGHT_GRAY.getRGB());
	}

	for(int x=24; x<img.getWidth(); x+=scale-1)
	{
		for(int y=0; y<img.getHeight();y++)
			img.setRGB(x, y,Color.LIGHT_GRAY.getRGB() );
		if (hour1 <=12){
		g.drawString(hour1+" AM", x+2 , img.getHeight()-5);
		hour1++;}
		else {
			g.drawString(hour2+" PM", x+2 , img.getHeight()-5);
			hour2++;
			
		}	
	}	
}

public static void drawDate(BufferedImage img,Color color,int y)
{
	Font font = new Font("Arial", Font.PLAIN, 10);
	Graphics g=img.getGraphics();
	g.setColor(color);
	g.setFont(font);
	float value=max;
	value=(value-24)/100;	
	for(int i=0;i<=11;i++){
	g.drawString((Math.round(value*(10-i))-1)+"", 6 , y+i*35);
	}
}




} 


    




